package com.example.demo.Controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping(value = "/redis")
public class RedisController {

    @Autowired
    private RedisTemplate redisTemplate;

    //增加
    @RequestMapping(value = "/add/{key}/{value}")
    public String add(@PathVariable(value = "key") String key,@PathVariable(value = "value") String value){

        //设置10s之后不能查询
        redisTemplate.opsForValue().set(key,value,10,TimeUnit.SECONDS);
        redisTemplate.opsForValue().get(key);

        //设置从第6个开始替换
        redisTemplate.opsForValue().set(key,"test",6);

        //判断是否之前已经存在
        System.out.println(redisTemplate.opsForValue().setIfAbsent(key,"multi1"));//false  multi1之前已经存在
        System.out.println(redisTemplate.opsForValue().setIfAbsent(key,"multi111"));//true  multi111之前不存在

        //设置多个值
        Map<String,String> maps = new HashMap<String, String>();
        maps.put("multi1","multi1");
        maps.put("multi2","multi2");
        maps.put("multi3","multi3");

        redisTemplate.opsForValue().multiSet(maps);
        List<String> keys = new ArrayList<String>();
        keys.add("multi1");
        keys.add("multi2");
        keys.add("multi3");
        System.out.println(redisTemplate.opsForValue().multiGet(keys));
        //是否存在
        System.out.println(redisTemplate.opsForValue().multiSetIfAbsent(maps));

        //根据Key获得值
        redisTemplate.opsForValue().set("test","hello world");
        System.out.println("***************"+redisTemplate.opsForValue().get("test"));

        //设置键的字符串值并返回其旧值
        redisTemplate.opsForValue().set("getSetTest","test");
        System.out.println(redisTemplate.opsForValue().getAndSet("getSetTest","test2"));


        //为多个健取出他们的值
        Map<String,String> maps1 = new HashMap<String, String>();
        maps.put("multi1","multi1");
        maps.put("multi2","multi2");
        maps.put("multi3","multi3");
        redisTemplate.opsForValue().multiSet(maps1);
        List<String> keys1 = new ArrayList<String>();
        keys.add("multi1");
        keys.add("multi2");
        keys.add("multi3");
        System.out.println(redisTemplate.opsForValue().multiGet(keys1));





        return "success";
    }

    @RequestMapping(value = "/{key}")
    public Object getByKey(@PathVariable(value = "key") String key){

        ValueOperations valueOperations = redisTemplate.opsForValue();
        Object luo = valueOperations.get(key);
        valueOperations.getAndSet("luo","wang");

        return luo.toString();
    }

    @RequestMapping(value = "/test")
    public String test(){

        Map<String,String> maps = new HashMap<String, String>();
        maps.put("multi1","multi1");
        maps.put("multi2","multi2");
        maps.put("multi5","multi3");

        redisTemplate.opsForValue().multiSet(maps);
        System.out.println(redisTemplate.opsForValue().multiSetIfAbsent(maps));

        return "success";
    }
}
